# 此文件作用
# Input：需要新加入库的已经处理过的excel文件，（内容分类，实体事件，难度）
# Output: 

import json
import time

import xlrd

import utils.util
from config import HOTLINE_DIFFICULTY_LIBRARY, doc_dict_dir, data_incident, POLICE_INCIDENT, \
    MUNICIPAL_HEALTH_COMMISSION_INCIDENT, NATURAL_RESOURCES_AND_PLANNING_BUREAU_INCIDENT, \
    KEY_PROJECT_CONSTRUCTION_MANAGEMENT_DIVISION_INCIDENT


def add_to_library():
    # 原库的文件
    f_0 = open(OLD_LIBRARY, encoding="utf-8")
    info = json.load(f_0)
    # 待加入库的数据表
    utils.util.file_sorted(input_filename=ADD_TABLE)  # 排序
    f_1 = xlrd.open_workbook(ADD_TABLE, encoding_override="utf-8")
    table1 = f_1.sheets()[SHEET]
    # 新库的文件
    f_2 = open(NEW_LIBRARY, "w+", encoding="utf-8")

    for i in range(1, table1.nrows):
        print()
        print("第", i, "次")
        dict_1 = dict()
        list_2 = []
        dict_3 = dict()
        list_4 = []
        # 内容分类
        dict_3['type'] = table1.cell_value(i, CONTENT_CLASSIFICATION_COL)  # 分类
        print(dict_3['type'], end="  ")
        dict_1['name'] = table1.cell_value(i, INCIDENT_COL)  # 标题INCIDENT
        dict_1['level'] = table1.cell_value(i, DIFFICULTY_COL)  # 难度
        flag = False
        # 原难度库info，j表示一个dict_3类型
        for j in info:
            # print("原库循环中当前位置的type:", j['type'])
            # 如果原库中已经存在该type
            if dict_3['type'] == j['type']:
                flag = True
                flag_1 = False
                # keyword中匹配是否有重复的的name,如果没有重复
                for m in j['keyword']:
                    if dict_1['name'] == m['name']:
                        print("有相同的incident")
                        # flag = True
                        flag_1 = True
                        break
                    else:
                        # print("原库中存在该type,不存在对应的incident", dict_1['name'])
                        # continue
                        flag_1 = False
                        # list_2.append(dict_1)

                if not flag_1:
                    j['keyword'].append(dict_1)
                    # res = m['level'] + res
                    j['type_score'] = int((j['type_score'] * len(j['keyword']) + dict_1['level'])
                                          / (len(j['keyword']) + 1))
                # else:
                #     continue
                break

            # 如果原库中不存在该type，不应该直接加入，可能原库后面有匹配的
            elif dict_3['type'] != j['type']:
                # print("新表type与原库的第", info.index(j) + 1, "个type不匹配，新表的type：", dict_3['type'])
                flag = False

        if not flag:
            list_2.append(dict_1)
            dict_3['type'] = table1.cell_value(i, CONTENT_CLASSIFICATION_COL)  # 分类
            dict_3['keyword'] = list_2
            dict_3['type_score'] = dict_1['level']
            list_4.append(dict_3)
            info.append(dict_3)

    # 再在info中，也就是所有的dict_3中加入一个type_count，方便后面对type进行排序，hash查找，做hash表
    # type_count为当前type类别的序号，与type相同，全局唯一
    for inner_dict in info:
        inner_dict['type_count'] = info.index(inner_dict) + 1

    json.dump(info, f_2, indent=4, separators=(',', ':'), ensure_ascii=False)
    print(time.time() - start)
    print(list_4)


if __name__ == '__main__':
    OLD_LIBRARY = doc_dict_dir + HOTLINE_DIFFICULTY_LIBRARY
    # 注意更改新添加的文件名
    ADD_TABLE = data_incident + POLICE_INCIDENT
    NEW_LIBRARY = doc_dict_dir + '1.json'
    # 待加入库的数据文件sheet号
    SHEET = 0
    # 内容分类的列数
    CONTENT_CLASSIFICATION_COL = 1
    # 实体事件的列数
    INCIDENT_COL = 2
    # 难度值的列数
    DIFFICULTY_COL = 3
    start = time.time()
    add_to_library()
